package com.huifaapp.mall.view;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

public class ClickImageView extends ImageView{
	private Animator anim1;
    private Animator anim2;
    private int mHeight;
    private int mWidth;
    private float mX, mY;
    private float uX,uY;
    private Handler mHandler = new Handler();
 
    private OnClickListener listener;
 
    public ClickImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        mX = getX();
        mY = getY();
    }
 
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = getHeight() - getPaddingTop() - getPaddingBottom();
        mWidth = getWidth() - getPaddingLeft() - getPaddingRight();
        
    }
 
    private void init() {
 
        PropertyValuesHolder valueHolder_1 = PropertyValuesHolder.ofFloat(
                "scaleX", 1f, 0.9f);
        PropertyValuesHolder valuesHolder_2 = PropertyValuesHolder.ofFloat(
                "scaleY", 1f, 0.9f);
        anim1 = ObjectAnimator.ofPropertyValuesHolder(this, valueHolder_1,
                valuesHolder_2);
        anim1.setDuration(200);
        anim1.setInterpolator(new LinearInterpolator());
 
        PropertyValuesHolder valueHolder_3 = PropertyValuesHolder.ofFloat(
                "scaleX", 0.9f, 1f);
        PropertyValuesHolder valuesHolder_4 = PropertyValuesHolder.ofFloat(
                "scaleY", 0.9f, 1f);
        anim2 = ObjectAnimator.ofPropertyValuesHolder(this, valueHolder_3,
                valuesHolder_4);
        anim2.setDuration(200);
        anim2.setInterpolator(new LinearInterpolator());
    }
 
//    public void setOnClickListener(OnClickListener clickListener) {
//        this.listener = clickListener;
//    }
    @Override
    public void setOnClickListener(@Nullable OnClickListener listener) {
        this.listener=listener;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        	uX = event.getX();
        	uY = event.getY();
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    anim2.end();
                    anim1.start();
                }
            });
            break;
        case MotionEvent.ACTION_MOVE:
        	uX = event.getX();
        	uY = event.getY();
            break;
        case MotionEvent.ACTION_UP:
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    anim1.end();
                    anim2.start();
                }
            });
            if (listener != null && innerImageView()) {
 
                listener.onClick(this);
            }
            //EventBus.getDefault().post(BusEvent.TYPE);
            break;
        case MotionEvent.ACTION_CANCEL:
        	 mHandler.post(new Runnable() {
                 @Override
                 public void run() {
                     anim1.end();
                     anim2.start();
                 }
             });
            break;
        }
        return true;
    }
 
    //按下的点是否在View内
    public boolean innerImageView() {
 
        if (uX >= mX && uX <= mX + mWidth) {
            if (uY >= mY && uY <= mY + mHeight) {
                return true;
            }
        }
        return false;
    }
 
    /**
     *  点击事件处理回调　
     * @author renzhiqiang
     *
     */
    public interface ClickListener {
 
        public void onClick();
    }
 
    @Override
    protected void onDetachedFromWindow() {
        // TODO Auto-generated method stub
        super.onDetachedFromWindow();
    }
}
